package com.vids.service.impl;

import com.vids.model.entity.Register;
import com.vids.model.entity.SystemTime;
import com.vids.model.response.ResponseStatus;
import com.vids.mapper.SystemTimeMapper;
import com.vids.runner.VIIDRunner;
import com.vids.service.IRegisterService;
import com.vids.task.RegisterMaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class RegisterServiceImpl implements IRegisterService {


    @Autowired
    private VIIDRunner viidRunner;

    @Resource
    SystemTimeMapper systemTimeMapper;

    @Resource
    RegisterMaker registerMaker;


    @Override
    public ResponseStatus register(Register vo) {

	    return registerMaker.register(vo);
    }

    @Override
    public ResponseStatus unRegister(Register vo) {
	    return registerMaker.unRegister(vo);
    }

    @Override
    public ResponseStatus keepLive(Register vo) {
	    return registerMaker.keepLive(vo);
    }

    @Override
    @Transactional
    public SystemTime time() {
        String selfViidServerId = viidRunner.getSelfViidServerId();
        SystemTime time = systemTimeMapper.findByVIIDServerID(selfViidServerId);
        if (time != null) {
            Date date = new Date();
            systemTimeMapper.updateByVIIDServerID(selfViidServerId, new Date());
            time.setLocalTime(date);
        }
        return time;
    }

}
